{% extends "smErp/base.html" %}

{% block css_style %}
    {% load staticfiles %}
    <link href="{% static 'smErp/css/brand_info.css' %}" type="text/css" rel="stylesheet">
    <link href="{% static 'smErp/assets/dropzone/basic.min.css' %}" type="text/css" rel="stylesheet">
    <link href="{% static 'smErp/assets/dropzone/dropzone.min.css' %}" type="text/css" rel="stylesheet">

{% endblock %}



{% block content %}
    <div class="inner wrp">

        <div class="page_nav">
            <a href="{% url 'smErp:manage' %}" class="jsUrlLink" data-url="{% url 'smErp:manage' %}" target="_self">管理中心</a>
            <span class="gap">/</span>
            <a href="{% url 'smErp:brand_info' brand.id %}" class="jsUrlLink" data-url="{% url 'smErp:manage' %}" target="_self">品牌详情</a>
            <span class="gap">/</span>
            修改证书
        </div>

        {# 品牌头 #}
        <div class="brand_box">
            <div id="logo">
                <img src="{{ brand.logo }}">
            </div>
            <label id="brand_name">{{ brand.name_zh }}</label>
        </div>


        <div class="container_box cell_layout">
            <div class="processor" id="process">
                <ol>
                    <li class="current size1of1">
                        <div class="step_line"></div>
                        <div class="step_inner">
                            <i class="icon_step">1</i>
                            <h4>修改资料</h4>
                        </div>
                    </li>
                    <li class="size1of2 last">
                        <div class="step_line"></div>
                        <div class="step_inner">
                            <i class="icon_step">2</i>
                            <h4>提交成功</h4>
                        </div>
                    </li>
                </ol>
            </div>
            <div class="step_panel text_padding">
                <div class="app_create_box app" id="step1">
                    <form class="form" id="form" novalidate="novalidate">
                        {% csrf_token %}
                        <div class="frm_control_group" id="js_name_group">
                            <label for="" class="frm_label">证书名称</label>
                            <div class="frm_controls">
                                <span class="frm_input_box">
                                    <input name="name" id="name" class="frm_input" type="text" value="{{ license.name }}">
                                </span>
                            </div>
                        </div>
                        <div id="name_upload" class="dynamic_box"></div>
                        <div class="frm_control_group">
                            <label for="" class="frm_label">证书类型</label>
                            <div class="frm_controls">
                                <select id="type" class="frm_select_box" value="" name="type" lay-ignore>
                                    {% for type in license.TYPE_CHOIES %}
                                        {% if type.0 == license.type %}
                                            <option value ="{{ type.0 }}" selected="selected" class="frm_select button">{{ type.1 }}</option>
                                        {% else %}
                                            <option value ="{{ type.0 }}" class="frm_select button">{{ type.1 }}</option>
                                        {% endif %}
                                    {% endfor %}
                            </select>
                            </div>
                        </div>
                        <div class="frm_control_group textarea">
                            <label for="" class="frm_label">描述信息</label>
                            <div class="frm_controls">
                                <span class="frm_textarea_box">
                                    <textarea name="description" id="description" class="frm_textarea" maxlength="800">{{ license.description }}</textarea>
                                </span>
                                <p class="frm_tips">最多800字</p>
                            </div>
                        </div>
                        <div class="frm_control_group ">
                            <label for="" class="frm_label">证书图标</label>
                            <div class="frm_controls">
                                <img id="icon_box" style="width: 150px;height: 150px;margin-top: 20px;" src="{{ license.icon }}" alt="logo" onclick="upload_icon_img()">
                            </div>

                            <div class="frm_controls" style="display: none;">
                                <div class="upload_wrp">
                                    <!--文件上传-->
                                    <div class="upload_box has_demo">
                                        <div id="icon" class="dropzone">
                                            <div class="am-text-success dz-message">
                                                选择文件
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="frm_control_group ">
                            <label for="" class="frm_label">证书图标</label>
                            <div class="frm_controls">
                                <img id="img_box" style="width: 150px;height: 150px;margin-top: 20px;" src="{{ license.img }}" alt="logo" onclick="upload_license_img()">
                            </div>

                            <div class="frm_controls" style="display: none;">
                                <div class="upload_wrp">
                                    <!--文件上传-->
                                    <div class="upload_box has_demo">
                                        <div id="img" class="dropzone">
                                            <div class="am-text-success dz-message">
                                                选择文件
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="tool_bar border with_form">
                            <a href="javascript:void(0);" class="btn btn_primary jsSendBt" id="brandBtn">提交</a>
                        </div>
                    </form>
{#                    <form action="/file-upload" class="dropzone" id="my-awesome-dropzone"></form>#}
                </div>

                <div class="app_create_box result" id="step2" style="display:none">
                    <div class="page_msg simple default">
                        <div class="inner group">
                            <span class="msg_icon_wrp">
                                <i class="icon_msg success"></i>
                            </span>
                            <div class="msg_content">
                                <h4>您的品牌证书已成功提交审核，预计在7天内完成审核</h4>
                                <p>
                                    审核通过后，你才可以申购溯码
                                </p>
                                <p class="spacing">
                                    <a href="{% url 'smErp:manage' %}" data-url="{% url 'smErp:manage' %}" target="_self" class="btn btn_primary jsUrlLink">查看管理中心</a>
                                </p>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
{% endblock %}

{% block footscript %}
    <script src="{% static 'smErp/assets/dropzone/dropzone.min.js' %}"></script>
    <script>

        // 存储上传的图片
        var upload_imgs = {} ;
        var id = 1;
        //
        var icon_obj = {} ;
        var img_obj = {} ;
        $("#email").blur(function(){
{#           alert("hello world");#}
            var email = $("#email").val();
{#            alert(email);#}
            if(!email){
{#                alert("hellow");#}
                $("#email-msg").css("color","red");
                $("#email-msg").html("请输入邮箱地址");
                $("#email-msg").show();
            }
        });

        Dropzone.autoDiscover = false;

        $("#icon").dropzone({
            url: "{% url 'smErp:image_upload' %}",
            addRemoveLinks: true,
            dictRemoveLinks: "x",
            dictCancelUpload: "x",
            dictRemoveFile:"删除",
            maxFiles: 1,
            maxFilesize: 5,
            acceptedFiles: "image/*",
            init: function() {
                icon_obj = this;
                this.on("success", function(file, data) {
                    console.log("File " + file.name + "uploaded");
{#                    this.addChildNode("<input value='" + file.name + "'/>");#}
                        var logo_obj = $("<input name='icon' style='display:none;' value='" + data.img_url + "' readonly/>");
                        $("#icon").append(logo_obj);
                        $("#icon_box").attr("src", data.img_url);
                        file.id = id;        // 换成服务端唯一的索引，可以后面删除对应的input
                        upload_imgs[file.id] = logo_obj;
                        id += 1;
                });
                this.on("removedfile", function(file) {
                    console.log(file);
                    console.log("File " + file.name + "removed");
                    upload_imgs[file.id].remove();
                });
            }
        });

        $("#img").dropzone({
            url: "{% url 'smErp:file_upload' %}",
            addRemoveLinks: true,
            dictRemoveLinks: "x",
            dictCancelUpload: "x",
            dictRemoveFile:"删除",
            maxFiles: 5,
            maxFilesize: 5,
            acceptedFiles: "image/*",
            init: function() {
                img_obj = this ;
                this.on("success", function(file, data) {
                    console.log("File " + file.name + "uploaded");
{#                    this.addChildNode("<input value='" + file.name + "'/>");#}
                        var logo_obj = $("<input name='img' style='display:none;' value='" + data.img_url + "' readonly/>");
                        $("#img").append(logo_obj);
                        $("#img_box").attr("src",data.img_url);
                        file.id = id;        // 换成服务端唯一的索引，可以后面删除对应的input
                        upload_imgs[file.id] = logo_obj;
                        id += 1;
                });
                this.on("removedfile", function(file) {
                    console.log("File " + file.name + "removed");
                    upload_imgs[file.id].remove();
                });
            }
        });

        $("#brandBtn").on("click", function(){
            var brand_license_info = $("#form").serialize();
            var params = $.param({"action": "change", "license": "{{ license.id }}"}) + "&" + brand_license_info;
            $.post("{% url 'smErp:brand_license' brand.id %}", params,
                    function(data, status, xhr) {
                        if(status == "success") {
                            if(data.result == 0) {
                                // 提交品牌信息
                                // 切换状态
                                // 改变进度 栏状态
                                $("#step1").hide();
                                $("#step2").show();
                            }else{
                                layer.msg(data.reason);
                            }
                        }else{
                            layer.msg(status);
                        }

                    },
            "json");
        });

        function upload_icon_img(){
            console.log("进行点击操作");
            icon_obj.removeAllFiles();
            $("#icon").click();
        }

        function upload_license_img(){
            console.log("进行点击操作");
            img_obj.removeAllFiles();
            $("#img").click();
        }
    </script>
{% endblock %}




